package view.layer
{
	//选桌子
	
	import command.UserSitCommond;
	
	import event.GlobalEvent;
	import event.UserSitEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import flash3.framework.Layer;
	import flash3.net.MsgPacket;
	import flash3.net.NetFacade;
	
	import global.Resource;
	
	import model.*;
	
	import net.*;
	
	import view.plugin.FloatText;
	import view.plugin.GlobalPanel;
	import view.plugin.LoaderView;
	import view.plugin.TableList;
	
	import vo.*;
	
	
	public class LayerSelTable extends Layer
	{
		private var btnQuickStart:SimpleButton;
		private var main:MovieClip;
		private var tableList:TableList;
		private var scaleRect:MovieClip;
		private var curAvatar:MovieClip;
		private var scaleRectWidth:Number;
		private var scaleRectHeight:Number;
		private var isQuickJoin:Boolean;
		private var isSitSucceed:Boolean;
		public function LayerSelTable()
		{
			super();
			
//			main = new SelTable();
			main = Resource.i.getMovieClip("seltable","SelTable");
			main.y = 80;
			scaleRect = main.scaleRect;
			scaleRect.visible = false;
			scaleRectWidth = scaleRect.width;
			scaleRectHeight = scaleRect.height;
			btnQuickStart = main.btnQuickStart;
			btnQuickStart.addEventListener(MouseEvent.CLICK, onClickBtnQuickStart);
			addChild(main);
		}
		
		private function onClickBtnLeave(evt:MouseEvent):void
		{
			changeState(L.L_Lobby);
		}

		private function onUserSit(evt:UserSitEvent):void
		{
			if(!isSitSucceed)
				userSit(evt.userSit);
		}
		
		public function onClickBtnQuickStart(evt1:MouseEvent):void
		{
			if(!isSitSucceed)
			{
				isQuickJoin = true;
				NetFacade.i.acall(UserGroup.MDM_GR_USER, UserGroup.SUB_GR_SIT_FAILED, {call:netNofitySitFailed});
//				NetFacade.i.send(UserGroup.MDM_GR_USER, UserGroup.SUB_GR_USER_AUTOSIT_REQ);
				NetFacade.i.send(FrameGroup.MDM_GF_FRAME, FrameGroup.SUB_GF_INFO, false);
			}
		}
		private function userSit(userSit:UserSit):void
		{
			LoaderView.i.show(true);
			LoaderView.i.text = "正在进入游戏，请稍后...";
			NetFacade.i.acall(UserGroup.MDM_GR_USER, UserGroup.SUB_GR_SIT_FAILED, {call:netNofitySitFailed});
			NetFacade.i.send(UserGroup.MDM_GR_USER, UserGroup.SUB_GR_USER_SIT_REQ, userSit);
			NetFacade.i.send(FrameGroup.MDM_GF_FRAME, FrameGroup.SUB_GF_INFO, false);
		}
		
		private function onPlayScaleRectEnd():void
		{
			scaleRect.gotoAndStop(0);
			scaleRect.visible = false;
			scaleRect.addFrameScript(scaleRect.totalFrames-1, null);
			curAvatar.removeChild(scaleRect);
			changeState(L.L_Game);
		}

		override public function enter():void
		{
			NetFacade.i.acall(UserGroup.MDM_GR_USER, UserGroup.SUB_GR_USER_STATUS, {call:netNofityUserStatus,forever:true});
			NetFacade.i.acall(FrameGroup.MDM_GF_FRAME, FrameGroup.SUB_GF_OPTION, {call:netNofityOption});
			TableList.i.x = 5.25;
			TableList.i.y = 80;
			TableList.i.updateData();
			TableList.i.addEventListener(UserSitEvent.USER_SIT, onUserSit);
			main.addChild(TableList.i);
			isQuickJoin = false;
			isSitSucceed = false;
			GlobalPanel.i.updateInfo();
			GlobalPanel.i.addEventListener(GlobalPanel.CLICK_LEAVE, onClickBtnLeave);
		}
		private function netNofitySitFailed(...parms):void
		{
			isQuickJoin = false;
			isSitSucceed = false;
			LoaderView.i.show(false);
			FloatText.i.start("进入房间失败！");
		}
		private function netNofityOption(...parms):void
		{
			if(isQuickJoin)
			{
				isSitSucceed = true;
				LoaderView.i.show(false);
				var userStatus:UserStatus = MeInfoModel.i.userStatus;
				TableList.i.verticalScrollPosition = TableList.i.getTableView(MeInfoModel.i.userStatus.wTableID).y - 100;
				var obj:Object = TableList.i.getChairView(MeInfoModel.i.userStatus.wTableID,MeInfoModel.i.userStatus.wChairID);
				curAvatar = obj.avatar as MovieClip;
				scaleRect.x = (curAvatar.width - scaleRectWidth) / 2;
				scaleRect.y = (curAvatar.height - scaleRectHeight) / 2;
				curAvatar.addChild(scaleRect);
				scaleRect.visible = true;
				scaleRect.gotoAndPlay(0);
				scaleRect.addFrameScript(scaleRect.totalFrames-1, onPlayScaleRectEnd);
			}
			else
			{
				changeState(L.L_Game);
			}
		}
		private function netNofityUserStatus(...parms):void
		{
		}
		
		override public function leave():void
		{
			NetFacade.i.rcall(UserGroup.MDM_GR_USER, UserGroup.SUB_GR_USER_STATUS);
			NetFacade.i.rcall(FrameGroup.MDM_GF_FRAME, FrameGroup.SUB_GF_OPTION);
			TableList.i.removeEventListener(UserSitEvent.USER_SIT, onUserSit);
			main.removeChild(TableList.i);
			GlobalPanel.i.removeEventListener(GlobalPanel.CLICK_LEAVE, onClickBtnLeave);
			scaleRect.addFrameScript(scaleRect.totalFrames-1, null);
			LoaderView.i.show(false);
		}
		
		override public function update(lastTime:int):void
		{
		}
		
	}
}